<?php

class HBase {
	/**
	 * Uses a namespace.
	 * A namespace ending with an asterisk '*' refers to a directory, otherwise it represents a PHP file.
	 * If the namespace corresponds to a directory, the directory will be appended
	 * to the include path. If the namespace corresponds to a file, it will be included (include_once).
	 * @param string namespace to be used
	 * @param boolean whether to check the existence of the class after the class file is included
	 * @throws TInvalidDataValueException if the namespace is invalid
	 */
	public static function using($namespace,$checkClassExistence=true) {
		$a = array();
		$a = explode('.',$namespace);		
		try {
			$str = SEPARATOR.str_replace('.',SEPARATOR,$namespace);			
			// Include
			if ((count($a) > 1) && ($a[count($a) - 1] == '*')) { // Include all											
				$folder = HBase::getParentFolder($str);				
				$str = str_replace('*', $folder, $str);											
				HBase::getClasses(LIBRARY_PATH.$str);
			}
			else { // include one
				try {
				require_once(LIBRARY_PATH.$str.'.php');
				}
				catch (Exception $ex) {
					throw new Exception("<h1>Wrong namespace syntax!</h1>".$ex->getMessage());
				}
			}
		}
		catch (Exception  $ex) {
			throw new Exception("<h1>Wrong Namespace Syntax!</h1>".$ex->getMessage());
		}
	}
	
	// Use for namespace all only
	public static function getParentFolder($namespace) {
		$a = array();
		$a = explode('.',$namespace);
		$folder_name = $a[count($a) - 2];
		return $folder_name;
	}
	
	public static function getClasses($path) {
		$dir = $path;		
		$count = 0; // Count the number of array.
		$a = array(); 
		if (is_dir($dir)) {
			if ($dh = opendir($dir)) {
				while (($file = readdir($dh)) !== false) {
					if ($file != "lisa.php" && strlen($file) > 4) {
						// Check if not folder then add
						if (count(explode('.',$file)) >= 2)
							$a[$count++] = $file;
					}
				}
			}
		}
		if (count($a) == 0) 
			die ("Failed to init the library classes. Please check the library area.");
		else {
			for ($i = 0; $i < count($a); $i++) {
				require_once($path.$a[$i]);
			}
		}
	}
}

// Call System Classes
HBase::using('Data.HSql');
HBase::using('Security.*');
HBase::using('HModule');
HBase::using('HModuleLoader');
HBase::using('HCommon');
HBase::using('HUtil');
HBase::using('HApplication');
HBase::using('HLayout');
HBase::using('Interfaces.*');
HBase::using('Web.Widget.*');
HBase::using('Web.UI.*');

?>